package com.base.cn.platform.os.controller.op.giftcard;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.giftcard.OpGiftCardCode;
import com.base.cn.platform.os.service.op.giftcard.OpGiftCardCodeBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 礼品卡编码
 * @author ll
 * @date 2018/5/14 9:18
 */
@RestController
@RequestMapping("/manage/op")
public class OpGiftCardCodeController extends BaseController {


    @Autowired
    private OpGiftCardCodeBiz opGiftCardCodeBiz;

    /**
     * 搜索礼品卡编码券
     * @param currentPage
     * @param opGiftCardCode
     * @return
     */
    @RequestMapping("/queryOpGiftCardCodeListPage")
    public PageInfo<OpGiftCardCode> queryOpGiftCardCodeListPage(@RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage,
                                                                @RequestBody OpGiftCardCode opGiftCardCode,
                                                                @RequestParam(value = "giftCardData",required = false,defaultValue = "false")boolean giftCardData,
                                                                @RequestParam(value = "userData",required = false,defaultValue = "false")boolean userData,
                                                                @RequestParam(value = "courseData",required = false) boolean courseData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return opGiftCardCodeBiz.queryOpGiftCardCodeListPage(page,opGiftCardCode,giftCardData,userData,courseData);
    }

    /**
     * 搜索礼品卡编码券
     * @param opGiftCardCode
     * @return
     */
    @RequestMapping("/queryOpGiftCardCodeList")
    public List<OpGiftCardCode> queryOpGiftCardCodeList(@RequestBody OpGiftCardCode opGiftCardCode,
                                                        @RequestParam(value = "giftCardData",required = false,defaultValue = "false")boolean giftCardData,
                                                        @RequestParam(value = "userData",required = false,defaultValue = "false")boolean userData,
                                                        @RequestParam(value = "courseData",required = false,defaultValue = "false") boolean courseData){
        return opGiftCardCodeBiz.queryOpGiftCardCodeList(opGiftCardCode,giftCardData,userData,courseData);
    }


    /**
     * 根据卡号和类型搜索礼品卡
     * @param code
     * @param type
     * @return
     */
    @RequestMapping("/queryGiftCardCodeByCode")
    public OpGiftCardCode queryGiftCardCodeByCode(@RequestParam("code")String code,
                                                  @RequestParam("type")Integer type){
        return opGiftCardCodeBiz.queryGiftCardCodeByCode(code,type);
    }


    /**
     * 修改使用状态
     * @param userCardId == id
     * @param status
     * @param userId
     * @return
     */
    @RequestMapping("/updateGiftCardStatusCode")
    public void updateGiftCardStatusCode(@RequestParam("userCardId")BigDecimal userCardId,
                                         @RequestParam("status")Integer status,
                                         @RequestParam("userId")BigDecimal userId){
        opGiftCardCodeBiz.updateGiftCardStatusCode(userCardId,status,userId);
    }

    /**
     * 激活礼卡
     * @param id 礼卡ID
     * @param userId 用户ID
     * @param code 礼卡编码
     * @param codePwd 礼卡密码
     * @return Map<String,Object>
     */
    @RequestMapping("/activateGiftCard")
    public Map<String,Object> activateGiftCard(@RequestParam(value = "id",defaultValue = "0",required = false) BigDecimal id,
                                               @RequestParam("userId")BigDecimal userId,
                                               @RequestParam(value = "code",required = false) String code,
                                               @RequestParam(value = "codePwd",required = false) String codePwd){
        OpGiftCardCode giftCardCode = null;
        if(DataUtil.idIsNotNull(id)){
            giftCardCode = opGiftCardCodeBiz.findById(id);
        }else{
            if(!StringUtils.isNotEmpty(code) || !StringUtils.isNotEmpty(codePwd)){
                return ResultUtil.ERROR("请输入礼卡的编号密码");
            }
            giftCardCode = opGiftCardCodeBiz.queryCardCodeByNo(code);
            if(ObjectUtils.isNotEmpty(giftCardCode)){
                if(!giftCardCode.getCodePwd().trim().equals(codePwd)){
                    return ResultUtil.ERROR("礼卡密码错误，激活失败");
                }
            }
        }
        if(!ObjectUtils.isNotEmpty(giftCardCode)){
            return ResultUtil.ERROR("该礼卡不存在");
        }
        if(DataUtil.idIsNotNull(giftCardCode.getUserId()) &&
                giftCardCode.getUserId().compareTo(userId) !=0){
            return ResultUtil.ERROR("该礼卡不属于当前登录账号");
        }

        if(giftCardCode.getStatus().intValue() != 1 ){
            return ResultUtil.ERROR("礼卡状态错误，激活失败");
        }
        if(giftCardCode.getUseType().intValue() != 1){
            return ResultUtil.ERROR("礼卡已被使用");
        }
        //过期时间状态，1时间段，2天数
        int timeType = giftCardCode.getTimeType().intValue();
        if(timeType==1 || (giftCardCode.getBeginTime()!=null && giftCardCode.getEndTime()!=null)){
            if(giftCardCode.getBeginTime().after(new Date())){
                return ResultUtil.ERROR("该礼卡暂时未到使用时间");
            }
            if(giftCardCode.getEndTime().before(new Date())){
                return ResultUtil.ERROR("该礼卡已过期，激活失败");
            }
        }
        try {
            giftCardCode =opGiftCardCodeBiz.activateGiftCard(giftCardCode,userId);
        }catch (Exception e){
            logger.error("========OpGiftCardCodeController.activateGiftCard()",e);
            return ResultUtil.ERROR();
        }
        return ResultUtil.SUCCESS("激活成功",giftCardCode);
    }
}
